package com.chief.transform;


import com.chief.source.TestBean;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class TransformBaseTest {

    public static void main(String[] args) throws Exception {

        //environment,此处使用连接远程服务器的方式，不使用打包上传
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.createRemoteEnvironment(
                "hadoop52",8081,"E:\\bigdata-start\\flink\\flinkCode\\target\\flink-1.0-SNAPSHOT.jar");


        //source
        DataStreamSource<String> sourceMap = environment.readTextFile("viewfs://mycluster/data1/hello.txt");

        //map 将其转对象TestBean输出
        SingleOutputStreamOperator<TestBean> map = sourceMap.map(new MapFunction<String, TestBean>() {
            @Override
            public TestBean map(String value) throws Exception {
                return new TestBean(value);
            }
        });

        //flatMap 只要第一个元素组成TestBean
        SingleOutputStreamOperator<TestBean> flatMap = sourceMap.flatMap(new FlatMapFunction<String, TestBean>() {
            @Override
            public void flatMap(String value, Collector<TestBean> out) throws Exception {
                String[] split = value.split(" ");
                out.collect(new TestBean(split[0]));
            }
        });

        //filter过滤掉第一个字符不大于1的字符
        SingleOutputStreamOperator<String> filter = sourceMap.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String value) throws Exception {
                return value.split(" ")[0].length() > 1;
            }
        });

        map.print("map");
        flatMap.print("flatMap");
        filter.print("filter");

        environment.execute("transform base test");

    }
}
